<template>
  <div>
    <van-nav-bar title="修改密码" left-text="返回" left-arrow @click-left="$router.go(-1)"/>
    <br/>
    <van-form ref="form" colon @submit="onSubmit">
      <van-cell-group inset>
        <van-field v-model="form.old" type="password" name="old" label="原始密码" placeholder="请输入原始密码" :rules="[{
                  required: true }]"/>
        <van-field v-model="form.new" type="password" name="new" label="新的密码" placeholder="请输入新的密码" :rules="[{
                  required: true }]"/>
        <van-field v-model="form.agn" type="password" name="agn" label="确认密码" placeholder="请确认新的密码" :rules="[{
                  required: true }]"/>
        <br/>
        <van-cell :border="false">
          <van-button round block type="info" native-type="submit">提交</van-button>
        </van-cell>
      </van-cell-group>
    </van-form>
  </div>
</template>

<script>
import {Dialog, Notify} from 'vant'

export default {
  data() {
    return {
      form: {
        old: null,
        new: null,
        agn: null,
      }
    }
  },
  methods: {
    onSubmit(form) {
      if (form.new === form.agn) {
        this.$requests.post('/user/code/update', form).then(resp => {
          if (resp.n) {
            Dialog.alert({
              message: '修改成功'
            }).then(() => {
              this.$cookies.remove('token')
              this.$router.replace('/login')
            })
          }
        })
      } else {
        Notify({ type: 'danger', message: '新密码两次输入不一致' });
      }

    }
  }
}
</script>
